// pages/module/hostApply/hostApply.js
import regeneratorRuntime from '../../../utils/runtime.js';
import img_list from '../../../utils/util.js';

import {
	getData,
	setData,
	clearData,
	dataKey
} from '../../../utils/storage.js';
import shareFun from '../../../utils/shareFun.js';
import {
	uploadFile
} from '../../../utils/upload.js';
import {
	wxPay,
	applyHost,
	updataPayStatus,
	checkHostReg
} from '../../../utils/imapi.js';


var app=getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		show: 0,
		numList: [{
			name: '基本资料'
		}, {
			name: '保证金'
		}, {
			name: '平台审核'
		}],
		selectList: [{
				name: '个人',
				is_select: 1
			},
			{
				name: '商家/企业',
				is_select: 0
			},
			{
				name: '公益机构',
				is_select: 0
			},
		],
		selectIndex: 0,
		wx_no: '',
		is_host: false,
		step_num: 0,
		is_select: 0,
		fm: '',
		zm: '',
		yy: '',
		ot: '',
		status: '审核中…',
		status_con: "您的主播认证正在审核中，请耐心等待",
		img_list: {},
		xueke_cert1: '',
		anchor_money:'0'
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: async function(options) {
		this.setData({
			img_list: img_list.img_list
		})
		shareFun.hidenShareMenu();

		
		this.setData({
			anchor_money: app.globalData.configs.anchor_earnest_money
		})

		this.is_upload = false
		var wx_no = "",
			is_host = 0,
			step_num = 0;
		const {
			data
		} = await checkHostReg();
		step_num = 0;
		console.log(data);
		this.setData({
			step_num:data
		});
		try {
			wx_no = await getData(dataKey.wx_no);
			is_host = await getData(dataKey.is_host);
		} catch (error) {

		}
		if (wx_no) {
			this.data.wx_no = wx_no;
			this.setData({
				// step_num,
				is_host,
				wx_no,
				show: 1
			})
		} else {
			this.setData({
				// step_num,
				show: 1
			})
		}
		
	},
	upLoad(e) {
		var that = this;
		if (this.is_upload) return;
		var type = e.currentTarget.dataset.type
		this.is_upload = true
		wx.chooseImage({
			count: 1,
			sizeType: ['original', 'compressed'],
			sourceType: ['album', 'camera'],
			success: async function(res) {
				const tempFilePaths = res.tempFilePaths
				that.is_upload = false;
				if (type == 1) {
					that.setData({
						is_load1: true
					})
				} else if (type == 2) {
					that.setData({
						is_load2: true
					})
				} else if (type == 3) {
					that.setData({
						is_load3: true
					})
				} else if (type == 4) {
					that.setData({
						is_load4: true
					})
				}
				const {
					data
				} = await uploadFile(tempFilePaths[0])
				if (type == 1) {
					that.setData({
						zm: data.indexOf('http') > -1 ? data : "http:" + data,
						is_load1: false
					})
				} else if (type == 2) {
					that.setData({
						fm: data.indexOf('http') > -1 ? data : "http:" + data,
						is_load2: false,
					})
				} else if (type == 3) {
					that.setData({
						yy: data.indexOf('http') > -1 ? data : "http:" + data,
						is_load3: false,
					})
				} else if (type == 4) {
					that.setData({
						ot: data.indexOf('http') > -1 ? data : "http:" + data,
						is_load4: false,
					})
				}

			},
			fail: function() {
				that.is_upload = false
			}
		})
	},
	async submit() {
		if (!this.data.zm) {
			wx.showToast({
				icon: 'none',
				title: '请上传身份证正面',
			})
			return
		}
		if (!this.data.fm) {
			wx.showToast({
				icon: 'none',
				title: '请上传身份证反面',
			})
			return
		}

		if (this.data.selectIndex > 0) {
			if (!this.data.yy) {
				wx.showToast({
					icon: 'none',
					title: '请上传营业执照',
				})
				return
			}
		}

		const {
			data
		} = await applyHost({
			org_type: this.data.selectIndex,
			id_cert_zm: this.data.zm,
			id_cert_fm: this.data.fm,
			cert1: this.data.yy,
			xueke_cert1: this.data.xueke_cert1
		})
		wx.showToast({
			title: '提交成功',
		})
		var step_num = (this.data.step_num + 1) > 2 ? 2 : (this.data.step_num + 1)
		this.setData({
			step_num,
		})
		await setData(dataKey.step_num, step_num);
	},
	async confirm() {
		var that = this;



		if (this.data.is_select != 1) {
			wx.showToast({
				icon: 'none',
				title: '请阅读并同意《星品直播协议》',
			})
			return;
		}
		if (that.data.clickPay) return;
		that.data.clickPay = 1
		let money= app.globalData.configs.anchor_earnest_money
		const {
			data
		} = await wxPay({
			price: money, // 
			in_out: 'I',
			fin_type: 'BZJ',
			approve_status: 'TJ',
			remark: '保证金'
		})
		console.log(data);
		wx.requestPayment({ //调起微信支付
			'timeStamp': data.timestamp,
			'nonceStr': data.nonceStr,
			'package': data.package,
			'signType': 'MD5',
			'paySign': data.paySign,
			'success': async function(res) {
				that.data.clickPay = 0
				await updataPayStatus({
					ref_f_order_no: data.prePayId,
					approve_status: 'C',
					
				})
				var step_num = (that.data.step_num + 1) > 2 ? 2 : (that.data.step_num + 1)
				that.setData({
					step_num,
				})
				await setData(dataKey.step_num, step_num);
			},
			'fail': function(res) {
				console.log(res)
				that.data.clickPay = 0;
				wx.showToast({
					title: '支付取消',
				})
			}
		})
	},
	chooseSelect() {
		this.setData({
			is_select: !this.data.is_select
		})
	},
	select(e) {
		var index = e.currentTarget.dataset.index;
		this.data.selectList.forEach(item => {
			item.is_select = 0
		})
		this.data.selectList[index].is_select = 1
		this.setData({
			selectIndex: index,
			selectList: this.data.selectList
		})
	},
	goAgreement() {
		// wx.navigateTo({
		//   url: '/pages/module/agreement/agreement',
		// })
		wx.downloadFile({
			url: 'https://img.xingkonglian.net/temp/zhiboxieyi.pdf',
			success: function(res) {
				var filePath = res.tempFilePath
				wx.openDocument({
					filePath: filePath,
					fileType: 'pdf',
					success: function(res) {
						console.log('打开文档成功');
					}
				})
			},
		})
	},
	know() {
		wx.switchTab({
			url: '/pages/tabbar/my/my',
		})
	},
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: async function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	}
})
